package com.yidu.boss.sys.controller;




import com.yidu.boss.sys.domain.Resource;

import com.yidu.boss.sys.domain.User;


import com.yidu.boss.sys.service.impl.ResourceServiceImpl;
import com.yidu.boss.sys.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private  ResourceServiceImpl resourceService;

    @RequestMapping("selectAll")
    @ResponseBody
    public List<User> selectAll() {
        return userService.selectByExample(null);
    }

    @RequestMapping("login")
    public String login() {
        return "login";
    }

    @RequestMapping("doLogin/toIndex")
    public String  doLogin(@RequestParam(value = "userName",required = false) String userName, @RequestParam(value = "password",required = false) String password, Model model,HttpServletRequest request) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken usernamePasswordToken=null;
        if (subject.isAuthenticated()){
            usernamePasswordToken = (UsernamePasswordToken) subject.getSession().getAttribute("userToken");
        }else{
             usernamePasswordToken = new UsernamePasswordToken(userName, password);
        }
        try {
            //执行登录 如果没有异常就说明ok
            subject.login(usernamePasswordToken);
            //查询一级和所有的资源
            List<Resource> resources = resourceService.selectByParentId("R0000");
            List<Resource> resources1 = resourceService.selectAll();
            //将结果添加到作用域中给前端
            model.addAttribute("sysAll",resources1);
            model.addAttribute("sys",resources);
            subject.getSession().setAttribute("userToken",usernamePasswordToken);
            request.setAttribute("user",userService.doLogin(usernamePasswordToken.getUsername()));
            return "index";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg","密码错误");
            return "login";
        } catch (UnknownAccountException e){
            model.addAttribute("msg","用户名错误");
            return "login";
        }catch (ShiroException e){
            return "login";
        }

    }

    @RequestMapping("access")
    public String  noAccess(){
        return "access";
    }

    @RequestMapping("test")
    public String  testJsp(){
        return "test";
    }


    @RequestMapping("outLongin")
    public String outLongin(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    @RequestMapping("iJsp")
    public String iJsp(){
        return "i";
    }
}
